// index.js
// const app = getApp()
const {
  envList
} = require('../../envList.js');
const util = require('../../utils/util')
let title = '1'
let text = '2'
let time = '2022年6月13日'
let commentuser = '的撒大那家是滴哦撒旦'
Page({
  data: {
    swiper_background: [{
        img: 'cloud://cloud1-5glocnmp62950062.636c-cloud1-5glocnmp62950062-1310108215/ads/banner.jpg',
        // link:'/pages/activity/index',
      },
      {
        img: 'cloud://cloud1-5glocnmp62950062.636c-cloud1-5glocnmp62950062-1310108215/ads/work.png',
        // link:'/pages/activity/index',
      },
      {
        img: 'cloud://cloud1-5glocnmp62950062.636c-cloud1-5glocnmp62950062-1310108215/ads/bisai.jpg',
        link: '/pages/activity/index',
      }



    ],
    noticelist: ['公告1', '公告2'],
    currentkey: '',
    pathlist: [{
        img: '/images/help.png',
        name: '帮个忙',
        path: '/pages/dohelp/index'
      },
      {
        img: '/images/work.png',
        name: '实习兼职',
        path: '/pages/work/index'
      },
      {
        img: '/images/organization.png',
        name: '找组织',
        path: '/pages/findOrganization/index'
      },
      {
        img: '/images/findteam.png',
        name: '找队友',
        path: '/pages/makeTeam/index'
      },
    ],

    tasklist: [{
      title: 'title',
      publisher: 'publisher',
      task: 'task',
      money: 'money',
      enddate: '2022',
      tele: 'tele',
      status: false
    }],
    dynamiclist: [],
    showTip: true,
  },
  getxiaoxituisong() {
    let _id = wx.getStorageSync('OPENID')
    wx.requestSubscribeMessage({
      tmplIds: ["Z8kjov8pifwoQgS4VDEN41oWU73mpUuuAR7Q2ANV1ac"],
    }).then(res => {
      console.log("获取消息推送授权成功", res);
    }).catch(err => {
      console.log("获取消息推送授权失败", err);
    })
  },
  senddd(){
    let _id = wx.getStorageSync('OPENID')
    this.sendMsg(_id, title, text, time, commentuser);
  },
  sendMsg(_id, title, text, time, commentuser) {
    wx.cloud.callFunction({
      name: "sendMessage", //调用发送消息的云函数
      data: {
        _id: _id, 
        title: title,
        text: text,
        time: time,
        commentuser: commentuser
      }
    }).then(res => {
      console.log("发送消息成功", res);
    }).catch(err => {
      console.log("发送消息失败", err);
    })
  },

  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true,
      })
      wx.showTabBar({})
      wx.setNavigationBarTitle({
        title: 'UnivLink'
      })
    }
    if (wx.getStorageSync('NICKNAME')) {
      this.setData({
        showTip: false,
      })
    } else {
      wx.hideTabBar({})
      wx.setNavigationBarTitle({
        title: '用户使用须知'
      })
      this.setData({
        showTip: true,
      })
    }
    this.getIndexData()
    this.getnoticeList()

  },
  getnoticeList() {
    wx.cloud.database().collection('noticelist').where({}).limit(3).get({
      success: res => {
        this.setData({
          noticelist: res.data
        })
      }
    })
  },
  getIndexData() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getIndexdata',
      success: (res) => {
        console.log(res)
        wx.hideLoading({
          success: (res) => {},
        })
        let dynamicData = res.result.dynamiclist.list.map(item => {
          item.title = util.subStrFormat(item.title, 16)
          item.like = item.love.length
          return item
        })
        let tasklist = res.result.tasklist.list.map(item => {
          item.title = util.subStrFormat(item.title, 16)
          item.date = util.formatTime(new Date(item.date))
          return item
        })
        let internlist = res.result.internlist.data

        this.setData({
          dynamiclist: dynamicData,
          tasklist: tasklist,
          internlist: internlist
        })
        console.log(this.data.dynamiclist)
      },
      fail: function (err) {
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none',
        })
        console.error(err)
      },
      complete: res => {}
    })
  },
  gotargetpath(e) {
    let path = e.currentTarget.dataset.path
    console.log(path)
    wx.navigateTo({
      url: path,
    })
  },
  goDynamic() {
    wx.switchTab({
      url: '/pages/dynamic/index',
    })
  },
  getsearchResult(e) {
    let searchkey = e.detail
    if (searchkey === '') {
      return
    }
    let that = this
    wx.navigateTo({
      url: `/pages/searchResult/index?key=${searchkey}`,
      success: (result) => {
        that.setData({
          currentkey: '',
        })
      },
      fail: () => {},
      complete: () => {

      }
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        wx.setStorageSync('NICKNAME', res.userInfo.nickName)
        wx.setStorageSync('AVATARURL', res.userInfo.avatarUrl)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
        })
        this.onLoad()
      },
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
});